package com.suoluo.zenapp.controllers.api.dest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.suoluo.zenapp.common.encrypt.EncryptUtil;

//api/dest.do
public class Test {

	public static void main(String[] args) throws IOException {

		
		// try {
		URL url = new URL("http://localhost:8080/zenapp/api/dest/trip");
		HttpURLConnection httpConnection = (HttpURLConnection) url
				.openConnection();
		httpConnection.setRequestMethod("POST");
		long time = System.currentTimeMillis();
		String token = "aaaaa";
		StringBuilder builder = new StringBuilder(token);
		builder = builder.append(time);
		String secret = EncryptUtil.md5(builder.toString());
		StringBuffer buf = new StringBuffer("destId=27&secret=");
		//buf.append(secret).append("&time=").append(time).append("&resourceId=30");
		//buf.append(secret).append("&time=").append(time);
		buf.append(secret).append("&time=").append(time).append("&dataVersion=1&tripId=15");
		//buf.append(secret).append("&time=").append(time).append("&typeId=1");
//		Gson gson = new Gson();
//		String value = gson.toJson();
//		httpConnection.setRequestProperty("Connection", "Keep-Alive");
//		httpConnection.setRequestProperty("Content-Length",
//				String.valueOf(value.getBytes().length));
//		httpConnection.setRequestProperty("action", "dest");
		httpConnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
		httpConnection.setDoOutput(true);
		httpConnection.setDoInput(true);
		/*
		 * PrintWriter printWriter = new
		 * PrintWriter(httpConnection.getOutputStream());
		 * printWriter.print(parameters); printWriter.close();
		 */
//		OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
//				httpConnection.getOutputStream(), "utf-8");
//		outputStreamWriter.write(value);
//		outputStreamWriter.flush();
//		outputStreamWriter.close();
		OutputStream os = httpConnection.getOutputStream();
		String value = buf.toString();
		os.write(value.getBytes("utf-8"));
		os.flush();
		os.close();

		// code =;
		// } catch (Exception e) {
		// e.printStackTrace()
		// }
		if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
			StringBuffer stringBuffer = new StringBuffer();
			try {
				String strCurrentLine;
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(httpConnection.getInputStream()));
				while ((strCurrentLine = reader.readLine()) != null) {
					stringBuffer.append(strCurrentLine).append("\n");
				}
				reader.close();
			} catch (IOException e) {
			}
			System.out.println(stringBuffer);
		}

	}
}
